<template>
  <div>
    <van-nav-bar
      title="商品详情"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
      fixed
    />
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="item in gallery" :key="item.id">
        <img :src="item.img_url" alt="" width="100%" />
      </van-swipe-item>
    </van-swipe>
    <Tips></Tips>
    <div class="info">
      <p>{{ info.name }}</p>
      <p>{{ info.goods_brief }}</p>
      <p>{{ info.retail_price | RMBformat }}</p>
    </div>
    <van-cell title="选择商品" is-link @click="isSkushow = true" />

    <div class="attribute">
      <h3>商品参数</h3>
      <ul>
        <li v-for="(item, index) in attribute" :key="index">
          <div class="l van-ellipsis">{{ item.name }}</div>
          <div class="r van-ellipsis">{{ item.value }}</div>
        </li>
      </ul>
    </div>
    <div v-html="goods_desc" class="goods_desc"></div>
    <div class="title0">
      <span>常见问题</span>
    </div>
    <ul class="issue">
      <li v-for="item in issue" :key="item.id">
        <h3>{{ item.question }}</h3>
        <p>{{ item.answer }}</p>
      </li>
    </ul>
    <div class="title0">
      <span>大家都在看</span>
    </div>
    <Products :goodsList="goodsList"></Products>

    <van-sku
      ref="skuRef"
      v-model="isSkushow"
      :sku="sku"
      :hide-stock="sku.hide_stock"
      :goods="goods"
    />
    <AppGoodActive
      @goToCart="goToCart"
      :goodsCount="goodsCount"
    ></AppGoodActive>
    <FanHui></FanHui>
  </div>
</template>

<script>
import Tips from "@/components/Tips.vue";
import Products from "@/components/Products.vue";
import AppGoodActive from "@/components/AppGoodActive.vue";
import FanHui from '@/components/FanHui.vue'
import {
  getGoodsDatail,
  getGoodsXiangGuan,
  getGoodsCount,
  addCart,
} from "@/request/api.js";
export default {
  components: {
    Tips,
    AppGoodActive,
    Products,
    FanHui
  },
  data() {
    return {
      isSkushow: false,
      sku: {
        tree: [],
        hide_stock: false,
        price: 0, // 默认价格（单位元）
        stock_num: 0, // 商品总库存
      },
      goods: {
        // 默认商品 sku 缩略图
        picture: "",
      },

      // 轮播图
      gallery: [],
      // 产品信息
      info: {},
      // 产品参数
      attribute: [],
      // 产品信息(图片)
      goods_desc: "",
      // 常见问题
      issue: [],
      // 相关产品列表
      goodsList: [],
      // 购物车数量
      goodsCount: 0,
      productList: [],
    };
  },
  created() {},
  watch: {
    "$route.query.id": {
      handler() {
        this.getData();
      },
      immediate: true,
    },
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    async getData() {
      const id = this.$route.query.id;
      // 获取商品详情
      const res = await getGoodsDatail({ id });
      if (res.errno == 0) {
        let { gallery, info, attribute, issue, productList } = res.data;
        this.gallery = gallery;
        this.info = info;
        this.goods_desc = info.goods_desc;
        this.attribute = attribute;
        this.issue = issue;
        this.productList = productList;
        // sku数据
        this.goods.picture = info.list_pic_url;
        this.sku.price = info.retail_price;
        this.sku.stock_num = info.goods_number;
      }
      // 相关产品
      const resp = await getGoodsXiangGuan({ id });
      if (resp.errno == 0) {
        this.goodsList = resp.data.goodsList;
      }
      // 获取购物车产品数量
      const resp2 = await getGoodsCount();
      if (resp2.errno == 0) {
        this.goodsCount = resp2.data.cartTotal.goodsCount;
      }
    },
    async goToCart() {
      //获取token
      const tokenStr = localStorage.getItem("token");
      if (!tokenStr) return this.$toast("请先登录！");

      // 执行加入购物车数据
      if (this.isSkushow) {
        const res = await addCart({
          goodsId: this.$route.query.id,
          productId: this.productList[0].id,
          number: this.$refs.skuRef.getSkuData().selectedNum,
        });
        if (res.errno == 0) {
          // 获取购物车数量
          this.goodsCount = res.data.cartTotal.goodsCount;
          this.$toast.success("添加成功");
          setTimeout(() => {
            // this.$router.push('/cart')
            this.isSkushow = false;
          }, 1000);
        }
      } else {
        this.isSkushow = true;
      }
    },
  },
};
</script>

<style lang="less" scoped>
img {
  display: block;
}
.info {
  background-color: #fff;
  padding: 0.1rem 0;
  text-align: center;
  line-height: 0.4rem;
  border-bottom: 2px solid #ccc;
  p:nth-child(1) {
    font-size: 0.2rem;
  }
  p:nth-child(2) {
    font-size: 0.14rem;
  }
  p:nth-child(3) {
    font-size: 0.16rem;
    color: darkred;
  }
}
.attribute {
  background-color: #fff;
  margin-top: 0.2rem;
  padding: 0.1rem 5%;
  h3 {
    line-height: 0.5rem;
    font-size: 0.16rem;
  }
  li {
    background-color: #efefef;
    height: 0.4rem;
    line-height: 0.4rem;
    display: flex;
    margin-bottom: 0.1rem;
    border-radius: 0.05rem;
    .l {
      width: 25%;
      text-align: center;
      margin-right: 0.1rem;
      font-size: 0.12rem;
    }
    .r {
      flex: 1;
      font-size: 0.14rem;
    }
  }
}
/deep/ .goods_desc {
  img {
    width: 100%;
    display: block;
  }
}
.title0 {
  width: 100%;
  background: #fff;
  height: 0.5rem;
  position: relative;
  &::before {
    content: "";
    width: 50%;
    height: 2px;
    background: #ccc;
    position: absolute;
    top: 50%;
    margin-top: -1px;
    left: 50%;
    margin-left: -25%;
  }
  span {
    width: 30%;
    position: relative;
    background: #fff;
    display: block;
    text-align: center;
    margin: 0 auto;
    height: 0.5rem;
    line-height: 0.5rem;
  }
}
.issue {
  padding: 0 4%;
  background: #fff;
  li {
    h3 {
      height: 0.3rem;
      line-height: 0.3rem;
      padding-left: 0.2rem;
      position: relative;
      font-weight: normal;
      &::before {
        content: "";
        width: 4px;
        height: 4px;
        background: darkred;
        display: inline-block;
        position: absolute;
        left: 0;
        top: 50%;
        margin-top: -2px;
      }
    }
    p {
      font-size: 0.12rem;
      line-height: 0.2rem;
      padding-left: 0.2rem;
    }
  }
}
</style>
